package QuizPackage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

public class MultipleChoice extends Question {
	
	String[] answers;
	String correct;
	String question;
	
	public MultipleChoice(int ID, String[] answers, String question, String correct) {
		super(ID);
		this.answers = answers;
		this.question = question; 
		this.correct = correct;
	}
	
	public MultipleChoice(int ID,ResultSet rs)
	{
		super(ID);
		try{
			question = rs.getString("question");
			correct = rs.getString("response");
			int numAnswers = 0;
			String a1 = rs.getString("a1");
			String a2 = rs.getString("a2");
			String a3 = rs.getString("a3");
			String a4 = rs.getString("a4");
			String a5 = rs.getString("a5");
			if(!a1.equals("")) numAnswers++;
			if(!a2.equals("")) numAnswers++;
			if(!a3.equals("")) numAnswers++;
			if(!a4.equals("")) numAnswers++;
			if(!a5.equals("")) numAnswers++;
			answers = new String[numAnswers];
			int pos = 0;
			if(!a1.equals(""))
			{
				answers[pos]=a1;
				pos++;
			}
			if(!a2.equals(""))
			{
				answers[pos]=a2;
				pos++;
			}
			if(!a3.equals(""))
			{
				answers[pos]=a3;
				pos++;
			}
			if(!a4.equals(""))
			{
				answers[pos]=a4;
				pos++;
			}
			if(!a5.equals(""))
			{
				answers[pos]=a5;
				pos++;
			}
		} catch (SQLException e){
			e.printStackTrace();
		}
		randomizeOrder();
	}
	
	private void randomizeOrder()
	{
		Random rand = new Random();
		for(int i = 0 ; i < answers.length ; i++)
		{
			int pos = rand.nextInt(answers.length - i);
			String temp = answers[i];
			answers[i] = answers[pos+i];
			answers[pos+i]=temp;
		}
	}

	@Override
	public String getHTML() {
		StringBuilder html = new StringBuilder();
		html.append("<p>"+question+"</p>\n");
		for (int i = 0; i < answers.length; i++) {
			if(i==0) html.append("<input type = \"radio\" name=\""+questionID+"\" value = \""+answers[i]+"\"/ checked>"+answers[i]+"<br/>");
			else html.append("<input type = \"radio\" name=\""+questionID+"\" value = \""+answers[i]+"\"/>"+answers[i]+"<br/>");
		}
		String result = new String(html);
		return result;
	}

	@Override
	public int checkAnswer(String[] params) {
		if (params[0].equals(correct)) return 1;
		return 0;
	}

	@Override
	public int totalPoints() {
		return 1;
	}

	@Override
	public String correctAnswer() {
		return correct;
	}

	public String toString() {
		return question;
	}
	
	/**
	 * Returns a String of HTML that describes the UI users
	 * should use to create this question.  Returns only input
	 * types and text...does NOT return a form.
	 */
	public static String createQHTML()
	{
		StringBuilder html = new StringBuilder();
		html.append("<p>Enter the question.</p>");
		html.append("<input name=\"question\" type=\"text\">");
		html.append("<p>Enter the correct response.</p>");
		html.append("<input name=\"response\" type=\"text\">");
		html.append("<p>Enter up to 4 additional choices in the order you would like them to appear.</p>");
		html.append("<input name=\"r1\" type=\"text\">");
		html.append("<input name=\"r2\" type=\"text\">");
		html.append("<input name=\"r3\" type=\"text\">");
		html.append("<input name=\"r4\" type=\"text\">");
		
		String result = new String(html);
		return result;
	}

	/**
	 * Enters the question with the form data from request
	 * into the proper question database table.  Will read
	 * the same input as created in createQHTML.
	 */
	public static void submitQuestion(int quizid,HttpServletRequest request)
	{
		QuizDBC dbc = new QuizDBC();
		String question = request.getParameter("question");
		String correct = request.getParameter("response");
		String a2 = request.getParameter("r1");
		String a3 = request.getParameter("r2");
		String a4 = request.getParameter("r3");
		String a5 = request.getParameter("r4");
		dbc.editTable("INSERT INTO multiplechoice VALUES(\""+question+"\",\""+correct+"\",\""+correct+"\",\""+a2+"\",\""+a3+"\",\""+a4+"\",\""+a5+"\","+quizid+");");
		//System.out.println("INSERT INTO multiplechoice VALUES(\""+question+"\",\""+correct+"\",\""+correct+"\",\""+a2+"\",\""+a3+"\",\""+a4+"\",\""+a5+"\","+quizid+");");
	}
}

